package com.gx.smart.building.ui.modify.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.gx.smart.base.BaseMVVMFragment;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.ModifyPasswordFragmentBinding;
import com.gx.smart.building.ui.login.activity.LoginActivity;
import com.gx.smart.building.ui.modify.viewmodel.ModifyPasswordViewModel;
import com.gx.smart.common.util.AppConfig;
import com.gx.smart.lib.event.LoadingMessageEvent;
import com.mylhyl.circledialog.CircleDialog;

import org.greenrobot.eventbus.EventBus;

public class ModifyPasswordFragment extends BaseMVVMFragment<ModifyPasswordFragmentBinding,
        ModifyPasswordViewModel> implements View.OnClickListener {
    private boolean isRefreshing;

    @Override
    protected Class<ModifyPasswordViewModel> onBindViewModel() {
        return ModifyPasswordViewModel.class;
    }

    @Override
    protected ModifyPasswordFragmentBinding onBindViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent, boolean attachToParent) {
        return ModifyPasswordFragmentBinding.inflate(inflater, parent, false);
    }

    @Override
    protected void initView(@NonNull View view) {
        binding.submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit:
                modify();
                break;
        }
    }

    private void modify() {
        String newPassword = binding.newPassword.getText().toString().trim();
        String confirmPassword = binding.confirmPassword.getText().toString().trim();

        if (TextUtils.isEmpty(newPassword)) {
            new CircleDialog.Builder()
                    .setTitle("提示")
                    .setText("新密码不能为空")
                    .setPositive("确定", null)
                    .show(getActivity().getSupportFragmentManager());
            return;
        }

        if (TextUtils.isEmpty(confirmPassword)) {
            new CircleDialog.Builder()
                    .setTitle("提示")
                    .setText("确认密码不能为空")
                    .setPositive("确定", null)
                    .show(getActivity().getSupportFragmentManager());
            return;
        }


        if (newPassword.length() < 6) {
            new CircleDialog.Builder()
                    .setTitle("提示")
                    .setText("密码过于简单,密码长度不得小于6位")
                    .setPositive("确定", null)
                    .show(getActivity().getSupportFragmentManager());
            return;
        }

        if (!newPassword.equals(confirmPassword)) {
            new CircleDialog.Builder()
                    .setTitle("提示")
                    .setText("确认密码与密码不一致")
                    .setPositive("确定", null)
                    .show(getActivity().getSupportFragmentManager());
            return;
        }

        EventBus.getDefault().post(new LoadingMessageEvent(true));
        if (!isRefreshing) {
            viewModel.modify(newPassword);
            isRefreshing = true;
        }

    }

    @Override
    protected void initObserver() {
        viewModel.success.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean result) {
                if (result) {
                    isRefreshing = false;
                    EventBus.getDefault().post(new LoadingMessageEvent(false));
                    SPUtils.getInstance().remove(AppConfig.LOGIN_PASSWORD);
                    SPUtils.getInstance().remove(AppConfig.USER_NAME);
                    SPUtils.getInstance().remove(AppConfig.LOGIN_TOKEN);
                    ActivityUtils.startActivity(new Intent(getActivity(), LoginActivity.class));
                    getActivity().finish();
                }
            }
        });

        viewModel.error.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean result) {
                if (result) {
                    isRefreshing = false;
                    EventBus.getDefault().post(new LoadingMessageEvent(false));
                }
            }
        });
    }
}
